package com.mc.moring.widge;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mc.moring.R;
import com.mc.moring.bean.CreditGetResultBean;
import com.mc.moring.util.DensityUtil;
import com.mc.moring.util.SharePreUtil;
import com.mc.moring.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class SingleSelectProductDialogView extends PopupWindow {

    View view;
    Context context;
    private PopupWindow popupWindow;
    private RecyclerView recycler_view;
    private TextView tv_confirm;

    private ProductAdapter adapter;
    private List<CreditGetResultBean.DataBean.ProductInfoListBean> list = new ArrayList<>();

    public SingleSelectProductDialogView(Context context, int selectedPosition) {
        this.context = context;
        this.selectedPosition = selectedPosition;
    }

    public void setList(List<CreditGetResultBean.DataBean.ProductInfoListBean> list) {
        this.list = list;
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    public void showSingleSelectDialog(Context context, View view) {
        this.context = context;
        View popuView = View.inflate(context, R.layout.pop_select_product, null);
        popupWindow = new PopupWindow(popuView, DensityUtil.getScreenWidth((Activity) context), ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha();
            }
        });
        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);

        recycler_view = popuView.findViewById(R.id.recycler_view);
        tv_confirm = popuView.findViewById(R.id.tv_confirm);
        recycler_view.setLayoutManager(new LinearLayoutManager(context));
        if (adapter == null) {
            adapter = new ProductAdapter();
        }
        recycler_view.setAdapter(adapter);

        ImageView iv_close = popuView.findViewById(R.id.iv_close);
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClick != null) {
                    itemClick.onItemClick(v, selectedPosition);
                }
                popupWindow.dismiss();
            }
        });

        backgroundAlpha1();
    }


    private void backgroundAlpha1() {//透明函数
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
        lp.alpha = (float) 0.5;
        ((Activity) context).getWindow().setAttributes(lp);
    }

    public int selectedPosition = 0;

    class ProductAdapter extends RecyclerView.Adapter<ViewHolder> {


        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(context)
                    .inflate(R.layout.adapter_select_product, parent, false);
            return new ViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
            CreditGetResultBean.DataBean.ProductInfoListBean item = list.get(position);
            if (position == list.size() - 1) {
                holder.ll_item.setBackgroundResource(R.drawable.btn_select_loan_bg_lock);
                holder.ll_child.setBackgroundResource(0);
                holder.tv_day.setText("120 Day en 4 Cuota");
                holder.tv_tip.setText("Pegue puntualmente para desbloquear mejor condiciones.");

                holder.iv_lock.setVisibility(View.VISIBLE);
                holder.iv_vip.setVisibility(View.GONE);
            } else {
                holder.ll_item.setBackgroundResource(R.drawable.btn_selector_loan);
                holder.ll_child.setBackgroundResource(R.drawable.btn_select_loan_bg_white);
                holder.ll_item.setSelected(position == selectedPosition);
                holder.iv_lock.setVisibility(View.GONE);

                holder.iv_vip.setVisibility(position == 0 ? View.VISIBLE : View.GONE);
                holder.tv_day.setText(item.getTotalLimitDays() + " días en " + item.getPeriods() + " sesiones");
                String minApplyAmountStr = "0";
                minApplyAmountStr = String.valueOf(item.getMinApplyAmount());
                String calResult = "0";
                calResult = String.valueOf(item.getMinApplyAmount() * item.getTotalRate());
                holder.tv_tip.setText("Pida prestados $" + StringUtils.formatNumberWithCommas(minApplyAmountStr)
                        + " y solo paga \n" + "$" + StringUtils.formatNumberWithCommas(calResult) + " de interés por día.");

            }
            holder.ll_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("----position2",String.valueOf(position));
                    SharedPreferences sharedPreferences = context.getSharedPreferences("ListChoose", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("ListChoose",String.valueOf(position));
                    editor.apply();
                    if (position != list.size() - 1) {
                        showVipTip(position);
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return list.size();
        }
    }

    public void showVipTip(int position) {
        String vip_tip = SharePreUtil.getStringData("vip_tip");
        if (TextUtils.isEmpty(vip_tip) && selectedPosition == 0) {
            JVipDialog jvDialogView = new JVipDialog(context, new JDialogCallback() {
                @Override
                public void cancel(View view) {
                    selectedPosition = position;
                    adapter.notifyDataSetChanged();
                }

                @Override
                public void confirm(View view) {
                }
            });
            SharePreUtil.saveStringData("vip_tip", "vip_tip");
            jvDialogView.show();
        } else {
            selectedPosition = position;
            adapter.notifyDataSetChanged();
        }
    }

    onItemClickListener itemClick;

    public void setItemClick(onItemClickListener itemClick) {
        this.itemClick = itemClick;
    }

    public interface onItemClickListener {
        void onItemClick(View view, int position);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        LinearLayout ll_item;
        LinearLayout ll_child;
        TextView tv_day;
        TextView tv_tip;
        ImageView iv_vip;
        ImageView iv_lock;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ll_item = itemView.findViewById(R.id.ll_item);
            ll_child = itemView.findViewById(R.id.ll_child);
            tv_day = itemView.findViewById(R.id.tv_day);
            tv_tip = itemView.findViewById(R.id.tv_tip);
            iv_vip = itemView.findViewById(R.id.iv_vip);
            iv_lock = itemView.findViewById(R.id.iv_lock);
        }
    }

    private void backgroundAlpha() {//透明函数
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
        lp.alpha = (float) 1.0;
        ((Activity) context).getWindow().setAttributes(lp);
    }

    public void dismiss() {
        super.dismiss();
    }
}
